package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlDictDtlMapper;
import com.zl.autocode.dto.autocode.dictDtl.ReqDictDtlAddDTO;
import com.zl.autocode.dto.autocode.dictDtl.ReqDictDtlDelDTO;
import com.zl.autocode.dto.autocode.dictDtl.ReqDictDtlDetailDTO;
import com.zl.autocode.dto.autocode.dictDtl.ReqDictDtlEditDTO;
import com.zl.autocode.dto.autocode.dictDtl.ReqDictDtlQueryDTO;
import com.zl.autocode.dto.autocode.dictDtl.ReqDictDtlStateDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlAddDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlDelDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlDetailDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlEditDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlQueryDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlStateDTO;
import com.zl.autocode.dto.autocode.dictDtl.ResDictDtlVO2;
import com.zl.autocode.entity.autocode.ZlDictDtl;
import com.zl.autocode.entity.autocode.po.ZlDictDtlVo;
import com.zl.autocode.service.autocode.IDictDtlService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;

/**
 * 字典明细
 * @author ZL
 */
@Service("autocodeDictDtlService")
public class DictDtlServiceImpl extends AutocodeBaseManager implements IDictDtlService {

    @Autowired
    private ZlDictDtlMapper dictDtlMapper;

    @Override
    public ResDictDtlQueryDTO queryDictDtlList(ReqDTO<ReqDictDtlQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDtlQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResDictDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResDictDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getDictDisplay1())){
            reqData.setDictDisplay1("%"+reqData.getDictDisplay1()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlDictDtlVo> list = dictDtlMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResDictDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResDictDtlDetailDTO queryDictDtlDetail(ReqDTO<ReqDictDtlDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDtlDetailDTO reqData = reqInfo.getReqData();
        ResDictDtlDetailDTO res = new ResDictDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlDictDtl view = dictDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResDictDtlAddDTO insertDictDtlInfo(ReqDTO<ReqDictDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDtlAddDTO reqData = reqInfo.getReqData();
        ResDictDtlAddDTO res = new ResDictDtlAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlDictDtl record = new ZlDictDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dictType",record.getDictType());
        paramMap.put("dictDtlNo",record.getDictDtlNo());
        //检查主键是否冲突
        if(dictDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        dictDtlMapper.insertSelective(record);
        
        ZlDictDtl keyInfo = dictDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDictDtlEditDTO updateDictDtlInfo(ReqDTO<ReqDictDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDtlEditDTO reqData = reqInfo.getReqData();
        ResDictDtlEditDTO res = new ResDictDtlEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlDictDtl record = new ZlDictDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        dictDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dictType",record.getDictType());
        paramMap.put("dictDtlNo",record.getDictDtlNo());
        ZlDictDtl keyInfo = dictDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDictDtlDelDTO deleteDictDtlInfo(ReqDTO<ReqDictDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDtlDelDTO reqData = reqInfo.getReqData();
        ResDictDtlDelDTO res = new ResDictDtlDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        dictDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResDictDtlStateDTO updateDictDtlState(ReqDTO<ReqDictDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDtlStateDTO reqData = reqInfo.getReqData();
        ResDictDtlStateDTO res = new ResDictDtlStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlDictDtl record = new ZlDictDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        dictDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dictType",record.getDictType());
        paramMap.put("dictDtlNo",record.getDictDtlNo());
        ZlDictDtl keyInfo = dictDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
}